package org.hydrakyoufeng.sc;

import java.io.StringReader;
import java.util.Properties;

import org.hydrakyoufeng.base.log.LogTool;
import org.hydrakyoufeng.base.tool.ParamsTool;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;

/**
 * 用于nacos读取的properties文件加载进系统中
 * @author HydraKyouFeng
 *
 */
public class NacosPropertiesIniter {

	/**
	 * 
	 * @param serverId
	 * @param dataId properties的dataId（注意这里不要添加.properties）
	 * @param group
	 */
	public void doInit(
			String serverAddr,
			String nameSpace,
			String group,
			String dataId,
			String username,
			String password) {
		Properties properties = new Properties();
        // 指定配置的 DataID 和 Group
//        String dataId = "params_redis_mybatis.properties";
//        String group = "DEFAULT_GROUP";
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        properties.put(PropertyKeyConst.NAMESPACE, nameSpace);
        properties.put(PropertyKeyConst.USERNAME, username);
        properties.put(PropertyKeyConst.PASSWORD, password);
		try {
			ConfigService configService = NacosFactory.createConfigService(properties);
			String strConfig = configService.getConfig(dataId + ".properties", group, 500);
			
			Properties temp = new Properties();
			temp.load(new StringReader(strConfig));
			ParamsTool.add(dataId, temp);
		} catch (Exception e) {
			LogTool.log(LogTool.ERROR, "无法加载nacos的properties文件：dataId = ",dataId);
			e.printStackTrace();
		}
	}
	
}
